package com.zhengqiangblog.blog.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.zhengqiangblog.blog.dao.common.JdbcDaoSupport;
import com.zhengqiangblog.blog.model.AdminAccount;
import com.zhengqiangblog.core.RowMapper;

public class AdminAccountDao extends JdbcDaoSupport {

	private AdminAccountRowMapper adminAccountRowMapper = new AdminAccountRowMapper();

	public AdminAccount login(String login, String password) throws Exception {
		String sql = "select * from admin_account where login = ? and password = password(?)";
		return this.getJdbcTemplate().queryForObject(sql, adminAccountRowMapper, login, password);
	}

	public int updateLastLoginDate(int id, Date lastLoginDate) throws Exception {
		String sql = "update admin_account set last_login_date = ? where id = ?";
		return this.getJdbcTemplate().update(sql, lastLoginDate, id);
	}

	private class AdminAccountRowMapper implements RowMapper<AdminAccount> {
		@Override
		public AdminAccount mapRow(ResultSet rs, int rowNum) throws SQLException {
			AdminAccount adminAccount = new AdminAccount();
			adminAccount.setId(rs.getInt("id"));
			adminAccount.setLogin(rs.getString("login"));
			adminAccount.setLastLoginDate(rs.getTimestamp("last_login_date"));
			return adminAccount;
		}
	}

}
